﻿using System;
using System.Collections.Generic;
using GameFramework.Battle.Core;
using Gameplay.PVE.Utils;
using RootMotion.FinalIK;
using UnityEngine;
using UnityEngine.PlayerLoop;
using Yoozoo.Managers;
using Random = System.Random;

namespace Gameplay.PVE.Survivor
{
    public class PassiveSkillComponent: ComponentBase
    {
        private Dictionary<int,List<PassiveSkill>> passiveSkills = new Dictionary<int, List<PassiveSkill>>();
        
        private List<PassiveSkill> castingSkills = new List<PassiveSkill>();
        protected override void OnInitialized()
        {
            castingSkills.Clear();
            if (unit.config.passive_skill != null)
            {
                for (int i = 0; i < unit.config.passive_skill.Count; i++)
                {
                    var config = BattleConfigManager.Instance.GetSkillConfig(unit.config.passive_skill[i]);
                    if (config != null)
                    {
                        var skill = ClassManager.Get<PassiveSkill>();
                        skill.Initialize(config, unit);
                        if (!passiveSkills.TryGetValue(config.passive_trigger_type,out var list))
                        {
                            list = new List<PassiveSkill>();
                            passiveSkills.Add(config.passive_trigger_type, list);
                        }
                        list.Add(skill);
                    }
                }
            }
        }
        
        protected override void OnUpdate()
        {
            for (int i = 0; i < castingSkills.Count; i++)
            {
                if (castingSkills[i].IsInSkill())
                {
                    castingSkills[i].Update();
                }
                else
                {
                    //ClassManager.Free(castingSkills[i]);
                    castingSkills.RemoveAt(i);
                    i--;
                }
            }
        }

        public void Trigger(EPassiveTriggerType type)
        {
            if (passiveSkills.TryGetValue((int)type, out var list))
            {
                for (int i = 0; i < list.Count; i++)
                {
                    //BattleDebug.LogError(unit.unitId + " Trigger Passive Skill " + list[i].config.id);
                    if (list[i].IsInSkill())
                    {
                        continue;
                    }

                    var target = SearchTargetHelper.GetSkillTarget(unit, list[i]);
                    if (target != null)
                    {
                        list[i].SetTarget(target);
                    }
                    list[i].Start();
                    castingSkills.Add(list[i]);
                }
            }
        }
        
        public void CastSkill(int skillId)
        {
            foreach (var item in passiveSkills)
            {
                for (int i = 0; i < item.Value.Count; i++)
                {
                    if (item.Value[i].config.id == skillId)
                    {
                        if (item.Value[i].IsInSkill())
                        {
                            continue;
                        }
                        var target = SearchTargetHelper.GetSkillTarget(unit, item.Value[i]);
                        if (target != null)
                        {
                            item.Value[i].SetTarget(target);
                        }
                        item.Value[i].Start();
                        castingSkills.Add(item.Value[i]);
                    }
                }
            }
        }
        
        protected override void OnDead()
        {
            base.OnDead();
            Trigger(EPassiveTriggerType.SelfDead);
        }
        public override void OnReset()
        {
            base.OnReset();
            castingSkills.Clear();
            foreach (var item in passiveSkills)
            {
                for (int i = 0; i < item.Value.Count; i++)
                {
                    ClassManager.Free(item.Value[i]);
                }
            }
            passiveSkills.Clear();
        }
    }
}
